<template>
    <div>
        <van-row style="margin-top: 1rem;"> 
            <van-col span="2" >
                <van-icon name="arrow-left" @click="exit" />
            </van-col>
        </van-row>
        <p>地址管理</p>
        <van-row>
            <van-col span="24" >
                <van-cell-group inset>
                    <!-- onAdd绑定了这个按钮 -->
                    <van-address-list
                            v-model="chosenAddressId"
                            :list="list"
                            :disabled-list="disabledList"
                            disabled-text="国外地址超出配送范围"
                            default-tag-text="默认"
                            @add="onAdd"
                            @edit="onEdit"
                            @select="setDefault"
                    />
                </van-cell-group>
              
            </van-col>
        </van-row>
        <van-row>
            <van-col>
                <van-goods-action>
                
                    <van-goods-action-button
                        type="info"
                        text="新增地址"
                    />
                </van-goods-action>
            </van-col>
        </van-row>
    </div>
</template>

<script>
    import { Toast ,Dialog} from 'vant';
    import {getUserAddress,delUserAddress,updateAddressDefault} from '@/api/index'

    export default {
        data() {
            return {
                username:'',
                chosenAddressId: '1',
                endlist:[],
                list: [
                    // {
                    //     id: '',
                    //     name: '',
                    //     tel: '',
                    //     address: ' ',
                    // },
                    // {
                    //     id: '',
                    //     name: '',
                    //     tel: '',
                    //     address: '',
                    // }
                   
                   
                ],
                    disabledList: [
                        {
                            id: '',
                            name: 'jack',
                            tel: '1320000000',
                            address: '英国',
                        },
                    ],
    
            };
        },
        methods:{
            //设置默认值
            setDefault(item,index){
                item["username"]=localStorage.getItem("username")
                    let q=item.address
                    let a=q.split('-')

                    item["ssq"]=a[0]+"-"+a[1]+"-"+a[2]
                    item["address"]=a[3]
                    
                    //更改默认状态
                    // delUserAddress(item).then(res=>{
                    //   
                    // })
                    updateAddressDefault(item).then(res=>{
                        if(res.data.status){
                            this.$router.go(0)
                        }
                      
                    })

                //  console.log(item,index);
            },
            exit(){
                this.$router.push("/homepage")
            },
            // onClickButton(){
            //     this.$router.push('/addAddress')
            // },
            onAdd(){
                this.$router.push('/addAddress')
            },
            //点击按钮删除该页面
            onEdit(item,index) {
                Dialog.confirm({
                    title: '提醒',
                    message: '是否删除?',
                }).then(() => {
                    // 打印id
                    item["username"]=localStorage.getItem("username")
                    let q=item.address
                    let a=q.split('-')

                    item["ssq"]=a[0]+"-"+a[1]+"-"+a[2]
                    item["address"]=a[3]
                    
                    //删除页面
                    delUserAddress(item).then(res=>{
                        if(res.data.status)
                            this.$router.go(0)
                    })
                    
                }).catch(()=>{
   
                    // console.log("取消删除");
                });
          
            },
            //得到用户地址
            getaddress(){
                getUserAddress(localStorage.getItem('username')).then(res=>{
                    this.endlist=res.data.data
                    this.newlist=[]
                    for (let index = 0; index < this.endlist.length; index++) {
                            this.newlist.push({ id: '',
                                                name: '',
                                                tel: '',
                                                address: ''
                                })
                    }
                    
                    for(let index = 0; index < this.endlist.length; index++) {
                        this.newlist[index].id=this.endlist[index].id
                        this.newlist[index].name=this.endlist[index].name
                        this.newlist[index].tel=this.endlist[index].phone
                        this.newlist[index].address=this.endlist[index].area+"-"+this.endlist[index].address
                        if(this.endlist[index].isdefault===1)
                            this.newlist[index].isDefault=true
                       
                    }

                    this.list=this.newlist

                })
            }
        },
        mounted(){
           // 调用
           this.getaddress()
        }
    }
</script>